<?php 

//connect to a DSN "myDSN" 
//$conn = odbc_connect('contaq','sa','sqladmin'); 
$conn = false;
if ($conn) 
{ 
  //the SQL statement that will query the database 


//  $query = "select campagna from contaqcampagne where cliente='ace' and attiva=1"; 
// $query = " select count(*)nominativi,campagna from contaqliste where campagna in (Select campagna from contaqcampagne where cliente='ace' and attiva=1) group by campagna";

$query = "select z1.Campagna,z1.nominativi,z2.esitati,
convert(nvarchar(10),(100*cast(cast(esitati as float)/cast(nominativi as float) as decimal(5,2))))+'%' as '%'
, z1.Url 
from 
(select COUNT(*)nominativi,t1.campagna,url 
from ContaQListe t1 inner join ContaQCampagne t2
on t1.Campagna=t2.Campagna
and t2.Attiva=1
group by t1.Campagna,url) z1
inner join 
(
select count(*)esitati,t1.campagna
from ContaQChiamate t1 inner join ContaQCampagne t2
on t1.Campagna=t2.Campagna
and t2.Attiva=1
and t1.Stato=1
group by t1.Campagna
)z2

on z1.Campagna=z2.Campagna";



	
  //perform the query 
  $result=odbc_exec($conn, $query); 

  echo "<table border=\"1\"><tr>"; 

  //print field name 
  $colName = odbc_num_fields($result); 
  for ($j=1; $j<= $colName; $j++) 
  {  
    echo "<th>"; 
    echo odbc_field_name ($result, $j ); 
    echo "</th>"; 
  } 

  //fetch tha data from the database 
  while(odbc_fetch_row($result)) 
  { 
    echo "<tr>"; 
    for($i=1;$i<=odbc_num_fields($result);$i++) 
    { 
      echo "<td>"; 
      echo odbc_result($result,$i); 
      echo "</td>"; 
    } 
    echo "</tr>"; 
  } 

  echo "</td> </tr>"; 
  echo "</table >"; 

  //close the connection 
  odbc_close ($conn); 
} 
else echo "odbc not connected";



$server = 'localhost\SQLEXPRESS';

// Connect to MSSQL
$link = mssql_connect($server, 'sa', 'sqladmin');

if (!$link) {
    die('Something went wrong while connecting to MSSQL');
}
if ($link) {
  print 'ok';
}
?> 

